<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 2015/3/4
 * Time: 19:47
 * Use :
 * File: AbstractColumn.php
 */

namespace ulue\core\db\base\schema;

use ulue\core\abstracts\AbstractCore;

class Column extends AbstractCore
{
    /**
     * @var boolean
     */
    const SIGNED = true;

    /**
     * @var boolean
     */
    const UNSIGNED = false;

    /**
     * @var boolean
     */
    const ALLOW_NULL = true;

    /**
     * @var boolean
     */
    const NOT_NULL = false;

    /**
     * Property name.
     * @var  string
     */
    public $name = null;

    /**
     * Property type.
     * @var  string
     */
    public $type;

    /**
     * Property length.
     * @var integer
     */
    public $length;

    /**
     * Property signed.
     * @var  bool
     */
    public $signed = false;

    /**
     * Property collation.
     * @var  string
     */
    public $collation;

    /**
     * Property allowNull.
     * @var  bool
     */
    public $allowNull = false;

    /**
     * Property default.
     * @var  string
     */
    public $default;

    /**
     * Property position.
     * @var  string
     */
    public $position;

    /**
     * Property comment.
     * @var  string
     */
    public $comment;

    /**
     * Property autoIncrement.
     * @var  boolean
     */
    public $autoIncrement = false;

    /**
     * Property key.
     * @var  string
     */
    public $key;

    /**
     * Property privilege.
     * @var  string
     */
    public $privilege;

    /**
     * Property priKey.
     * @var boolean
     */
    public $isPriKey     = false;

    public $isForeignKey = false;

    /**
     * @param array $column
     * @param null $position
     */
    public function __construct($column=[], $position=null)
    {
        if ($column && is_array($column))
        {
            $this->load($column);
        }

        $this->position = $position;
    }

    /**
     * @param array $data
     * @return $this;
     */
    public function load(array $data)
    {
        foreach ($data as $name => $value)
        {
            if (property_exists($this, $name))
            {
                $this->$name = $value;
            } else
            {
                throw new \DNotFoundException(get_class($this)." property [$name] does not exist!");
            }
        }

        return $this;
    }

    /**
     * Method to set property isPriKey
     * @param   boolean $isPriKey
     * @return  static  Return self to support chaining.
     */
    public function isPriKey($isPriKey=null)
    {
        if ( is_null($isPriKey) )
        {
            return $this->isPriKey;
        }

        if ($this->isPriKey = (bool)$isPriKey)
        {
            // $this->signed        = false;
            $this->allowNull     = false;
            $this->autoIncrement = true;
        }

        return $this;
    }


}// end class AbstractColumn